package com.yuan.farmerwork.ynblog.rest;

import com.yuan.farmerwork.ynblog.domain.*;
import com.yuan.farmerwork.ynblog.domain.pojo.*;
import com.yuan.farmerwork.ynblog.service.*;
import com.yuan.farmerwork.ynblog.utils.RedisService;
import com.yuan.farmerwork.ynblog.utils.constants.PageConstants;
import com.yuan.farmerwork.ynblog.utils.constants.RedisKeyConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 首页前端控制器.
 *
 * @author yjs
 * @date 2020/10/8 15:52
 */
@Controller
public class IndexController {
    
    @Autowired
    private BlogService blogService;

    @Autowired
    private YnMianService ynMianService;

    @Autowired
    private YnMainProjectsService ynMainProjectsService;

    @Autowired
    private RedisService redisService;

    /**
     * 首页.
     *
     * @param map 页面参数map
     * @return
     */
    @GetMapping("/")
    public ModelAndView getIndex(Map map) {

        //获取首页介绍
        List<YnMian> ynMians = ynMianService.list();
        map.put(PageConstants.PAGE_PARAM_YN_MAIN, ynMians.get(0).getMainTitle());

        //获取服务介绍
        List<YnMainProjects> mainProjectsList = ynMainProjectsService.list();
        map.put(PageConstants.PAGE_PARAM_MAIN_PROJECTS_LIST, mainProjectsList);

        //获取阅读榜单
        List<Blogs> readList = blogService.findReadmList();
        map.put(PageConstants.PAGE_PARAM_READ_LIST, readList);
        map.put(PageConstants.PAGE_PARAM_VISIT_NUM, redisService.get(RedisKeyConstants.REDIS_KEY_VISIT_NUM));

        return new ModelAndView(PageConstants.PAGE_INDEX, map);
    }

}
